package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLPurAdjQtyToF4QueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLPurAdjQtyToF4Mapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLPurAdjQtyToF4QueryCmdImpl extends CommandAdapter implements ViewDLPurAdjQtyToF4QueryCmd {

    @Autowired
    QueryCpUtil queryCpUtil;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLPurAdjQtyToF4Mapper viewDLPurAdjQtyToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLPurAdjQtyToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLPurAdjQtyToF4QueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        JSONArray jsonArray = new JSONArray();
        List<HashMap> list = viewDLPurAdjQtyToF4Mapper.selPurAdjQtyData(ids);
        for (HashMap hashMap : list) {
            Long id = TypeUtils.castToLong(hashMap.get("ID"));
            String billNo = TypeUtils.castToString(hashMap.get("BILL_NO"));

            if (null != hashMap.get("CP_C_STORE_ID")) {
                Long storeID = TypeUtils.castToLong(hashMap.get("CP_C_STORE_ID"));
                //跨库查询
                JSONObject whereJs = new JSONObject();
                whereJs.put("ID", storeID);
                JSONObject h = queryCpUtil.queryCP("CP_C_STORE", "ECODE", whereJs);

                if (null != h && h.size() > 0) {
                    hashMap.put("CP_C_STORE_ECODE", h.get("ECODE"));
                } else {
                    throw new NDSException("店仓 查询为空！  店仓ID：" + storeID);
                }
            }

            if (null != hashMap.get("CP_C_SUPPLIER_ID")) {
                Long supplierId = TypeUtils.castToLong(hashMap.get("CP_C_SUPPLIER_ID"));
                //跨库查询
                JSONObject whereJs = new JSONObject();
                whereJs.put("ID", supplierId);
                JSONObject h = queryCpUtil.queryCP("CP_C_SUPPLIER", "ECODE", whereJs);

                if (null != h && h.size() > 0) {
                    hashMap.put("CP_C_SUPPLIER_ECODE", h.get("ECODE"));
                } else {
                    throw new NDSException("供应商 查询为空！  供应商ID：" + supplierId);
                }
            }


            List<HashMap> itemList = viewDLPurAdjQtyToF4Mapper.selPurAdjQtyItemData(id);
            JSONArray itemArray = new JSONArray();
            for (HashMap itemH : itemList) {
                itemH.put("BILL_NO", billNo);
                //管理成本价
                BigDecimal priceMgt = TypeUtils.castToBigDecimal(itemH.get("PRICE_MGT"));
                //管理成本金额
                BigDecimal amtMgt = TypeUtils.castToBigDecimal(itemH.get("AMT_MGT"));
                //不含税采购价
                BigDecimal priceNonTax = TypeUtils.castToBigDecimal(itemH.get("PRICE_NON_TAX"));
                //不含税采购金额
                BigDecimal amtNonTax = TypeUtils.castToBigDecimal(itemH.get("AMT_NON_TAX"));
                if (null != priceMgt) {
                    itemH.put("PRICE_MGT", priceMgt);
                } else {
                    itemH.put("PRICE_MGT", 0);
                }

                if (null != amtMgt) {
                    itemH.put("AMT_MGT", amtMgt);
                } else {
                    itemH.put("AMT_MGT", 0);
                }

                if (null != priceNonTax) {
                    itemH.put("PRICE_NON_TAX", priceNonTax);
                } else {
                    itemH.put("PRICE_NON_TAX", 0);
                }

                if (null != amtNonTax) {
                    itemH.put("AMT_NON_TAX", amtNonTax);
                } else {
                    itemH.put("AMT_NON_TAX", 0);
                }
                JSONObject itemjs = new JSONObject(itemH);
                itemArray.add(itemjs);
            }

            JSONObject js = new JSONObject(hashMap);
            js.put("DL_B_PUR_ADJ_QTY_ITEM", itemArray);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                    "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);

        }
        log.debug("ViewDLPurAdjQtyToF4QueryCmdImpl--jsonArray-->{}",jsonArray);
//        log.info("ViewDLPurAdjQtyToF4QueryCmdImpl->" + jsonArray);
        vh.put("data", jsonArray);
        return vh;
    }

}
